<?php
/*
	Copyright 2006, 2007, 2008, 2009, 2010 Bastiaan Grutters
    
    This file is part of Ages of Strife website.

    Ages of Strife website is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    Ages of Strife website is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with Ages of Strife website.  If not, see <http://www.gnu.org/licenses/>.
 */
include( '../global/initialize.php' );

$query = "SELECT admin FROM users WHERE user_id = " . $_SESSION[ 'user_id' ];
$result = mysql_query( $query ) or die( "Query failed : " . mysql_error() );
$row = mysql_fetch_array($result, MYSQL_ASSOC);
if( isset( $row[ 'admin' ] ) && $row[ 'admin' ] == 1 ) {
?>
<html>
	<head>
		<title>
			Ages of Strife: Send Newsletter
		</title>
		<link type="text/css" rel="stylesheet" href="../global/agesofstrife.css" />
	</head>
	<body>
		<div id="root_container">
<?php
	include( "../global/ads.php" );
?>
			<div id="container">
<?php
	include_once( "../global/old_database_connection.php" );
?>
				<div id="main-content">
					<div class="blok">
						<strong>Send Newsletter</strong><br />
    
    <?php
    if( isset( $_SESSION[ 'newsletter_send_status' ] ) ) {
    	print( "Status: " . $_SESSION[ 'newsletter_send_status' ] . "<br /><br />\r\n" );
    	unset( $_SESSION[ 'newsletter_send_status' ] );
    }
    
	$query = "SELECT count(*) FROM users WHERE news_letter = 1 AND email != ''";
	$result = mysql_query( $query ) or die( "Query failed : " . mysql_error() );
	$row = mysql_fetch_array($result, MYSQL_ASSOC);
    $total = number_format( $row[ 'count(*)' ] );
    
    $query = "SELECT email, admin " .
    		"FROM users " .
    		"WHERE news_letter = 1 AND email != '' AND admin = 1 " .
    		"ORDER BY email LIMIT 10";
	$result = mysql_query( $query ) or die( "Query failed : " . mysql_error() );
	$num = mysql_numrows( $result );
	
	if( $num > 0 ) {
		print( "Subscriptions ($total):<br /><ul>" );
		$i = 0;
		while( $i < $num ) {
			if( mysql_result( $result, $i, 'admin' ) == 1 ) {
				$admin = " (Administrator)";
			}
			else {
				$admin = "";
			}
			print( "<li>" . mysql_result( $result, $i, 'email' ) . "$admin</li>" );
			$i ++;
		}
		print( "</ul>" );
		
		if( isset( $_SESSION[ 'news_letter_subject' ] ) ) {
			$subject = $_SESSION[ 'news_letter_subject' ];
		}
		else {
			$subject = "Ages of Strife newsletter: [Enter subject here]";
		}
		
		if( isset( $_SESSION[ 'news_letter_message' ] ) ) {
			$message = $_SESSION[ 'news_letter_message' ];
		}
		else {
			$message = "[Enter message here]\r\n" .
					"\r\n" .
					"------------------------------------------------------------------------\r\n" .
					"If you forgot your username or password you can retrieve it at: http://www.agesofstrife.com/support.php .\r\n" .
					"\r\n" .
					"If you do not want to receive this newsletter anymore log in to Ages of Strife and unsubscribe from the newsletter.\r\n" .
					"You can log into the game at: http://www.agesofstrife.com .\r\n" .
					"To change your settings go to Overview, Account settings (it's the link at the bottom), uncheck Ages of Strife newsletter and press Save settings.\r\n";
		}
		
		?>
						<form method="post" action="send_newsletter2.php">
							Subject:<br />
							<input type="textfield" size="80" value="<?php print( stripslashes( $subject ) ); ?>" name="subject" /><br />
							Message:<br />
							<textarea name="message" cols="80" rows="20"><?php print( html_entity_decode( stripslashes( $message ) ) ); ?></textarea>
							<br />
							<input type="checkbox" value="confirmed" name="confirm" /> Confirm sending<br />
							<input type="submit" value="Test" name="submit" /> <small>(Sends the newsletter to admins only)</small><br />
							<input type="submit" value="Send" name="submit" /> <small>(Sends the newsletter to all subscribed users)</small><br />
						</form>
		<?php
	}
	else {
		print( "There are no valid subscriptions to the news letter!<br />" );
	}
	?>
						<br />
						<a href="admin.php">Back</a>
					</div> <!-- close blok -->
				</div> <!-- close main content -->
			</div> <!-- close container -->
		</div> <!-- close root_container -->
	</body>
</html>
	<?php
}
else {
	$_SESSION[ 'admin' ] = 0;
	header( "Location: ../overview/overview.php" );
}
?>
